package com.school.homeServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.school.dao.NewsDao;
import com.school.dao.NewsDaoImpl;
import com.school.entity.News;
import com.school.homeServlet.cmd.AboutUsCommand;
import com.school.homeServlet.cmd.AdminRegistrationFormCommand;
import com.school.homeServlet.cmd.AppStatusFormCommand;
import com.school.homeServlet.cmd.CareerCommand;
import com.school.homeServlet.cmd.ContactUsCommand;
import com.school.homeServlet.cmd.ApplicantStatusCommand;
import com.school.homeServlet.cmd.GetAdmissionFormCommand;
import com.school.homeServlet.cmd.HomeCmd;
import com.school.homeServlet.cmd.HomeCommand;
import com.school.homeServlet.cmd.NewRegistrationFormCommand;
import com.school.homeServlet.cmd.NewStudentRegisterCommand;
import com.school.homeServlet.cmd.StudentAdmissionFormCommand;
import com.school.homeServlet.cmd.UserLoginCommand;


/**
 * Servlet implementation class HomeController
 * This servlet will act as Home controller which will accept request from and send response to all guest users.
 * @author KK
 */
	
	public class HomeController extends HttpServlet {

	public static Logger log = Logger.getLogger(HomeController.class.getSimpleName());
	
	private ServletConfig config;
	
	private Map<String, HomeCommand> commands;
	

	public void init(ServletConfig config) {
		log.info("Entered init(ServletConfig)");
		
			
		this.config = config;
		log.info("Cached the ServletConfig object");
		
		commands = new HashMap<String, HomeCommand>();
		commands.put("home", new HomeCmd());
		commands.put("login", new UserLoginCommand());
		commands.put("newRegistrationForm", new NewRegistrationFormCommand());
		commands.put("adminRegistrationForm", new AdminRegistrationFormCommand());
		commands.put("aboutUs", new AboutUsCommand());
		commands.put("contactUs", new ContactUsCommand());
		commands.put("applicantStatus", new ApplicantStatusCommand());
		commands.put("appStatusForm", new AppStatusFormCommand());
		
	//	commands.put("examResult", new ExamResultCommand());
	//	commands.put("interviewSchedule", new InterviewScheduleCommand());
	//	commands.put("interviewResult", new InterviewResultCommand());
		commands.put("newStudentRegister", new NewStudentRegisterCommand());
		commands.put("getAdmissionForm", new GetAdmissionFormCommand());
		commands.put("studentAdmissionForm", new StudentAdmissionFormCommand());
		commands.put("career", new CareerCommand());
		
		
		
		
		
		config.getServletContext().setAttribute("commands", commands);
		
		log.info("Exited init(ServletConfig)");
	}

	public void init() {
		log.info("Entered init()");
		log.info("Exited init()");
	}
		
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		log.info("Entered doGet()");
		
		NewsDao newsobject1 = new NewsDaoImpl();			/////////// (for displaying news)
		ArrayList<News> schnews = newsobject1.viewSchoolNews();	//////////
		
		req.setAttribute("schoolnews", schnews);			///////////
		
		processRequest(req, resp);
		log.info("Exited doGet()");
	}
		
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		log.info("Entered doPost()");
		
		NewsDao newsobject1 = new NewsDaoImpl();			/////////// (for displaying news)
		ArrayList<News> schnews = newsobject1.viewSchoolNews();	//////////
		
		req.setAttribute("schoolnews", schnews);			///////////
		
		processRequest(req, resp);
		log.info("Exited doPost()");
	}
	
	
	
	private void processRequest(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		log.info("Entered processRequest()");
/*		
		NewsDao newsobject1 = new NewsDaoImpl();			/////////// (for displaying news)
		ArrayList<News> schnews = newsobject1.viewSchoolNews();	//////////
		
		req.setAttribute("schoolnews", schnews);			///////////
*/

		String command = req.getParameter("cmd");
//		log.info("Command received is " + command);
		System.out.println("Command received is " + command);
		
		if (commands.get(command) == null) {
			command = "home";
		}
		commands.get(command).processRequest(req, resp, config);
		
		log.info("Exited processRequest()");
	}
}
